package parser.nodes;

import codegen.AbstractDescr;
import codegen.CodeGen;
import parser.TokenOr;
import scanner.Token;

// Type = ident | ArrayType | RecordType
public class TypeNode extends Node {
    private Token ident;
    private ArrayTypeNode arrayType;
    private RecordTypeNode recordType;

    public TypeNode(Token ident) {
        this.ident = ident;
    }

    public TypeNode(ArrayTypeNode arrayType) {
        this.arrayType = arrayType;
    }

    public TypeNode(RecordTypeNode recordType) {
        this.recordType = recordType;
    }
	
	public TokenOr<Node> getType() {
		if (ident != null)
			return new TokenOr<>(ident);
		else if (arrayType != null)
			return new TokenOr<>((Node)arrayType);
		else
			return new TokenOr<>((Node)recordType);
	}
    
    @Override
    public void print() {
        p("Type:");
        indent();
        if (ident != null) 
            p(ident);
        if (arrayType != null)
            arrayType.print();
        if (recordType != null)
            recordType.print();
        unindent();
    }

    @Override
    public void abstractPrint() {
        if (ident != null) 
            p(ident);
        if (arrayType != null)
            arrayType.abstractPrint();
        if (recordType != null)
            recordType.abstractPrint();
    }

    @Override
    public AbstractDescr getDescriptor(CodeGen c) throws CompileError {
        if (ident != null) 
            return c.findDescr(ident.toString());
        if (arrayType != null)
            return arrayType.getDescriptor(c);
        if (recordType != null)
            return recordType.getDescriptor(c);
        return null;
    }
    
    
}
